﻿using BotDetector.Core.Utilities;
using libsvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TFIDFExample;

namespace BotDetector.Core.SVMModule
{
    public class SvmFileOperator
    {
        private string NONBOT_FOLDER_PATH;
        private string BOT_FOLDER_PATH;
        private string CLASSIFIER_CONFIG_FILE;
        private string CLASSIFIER_CONFIG_TRAIN_FILE;
        private string IDF_PATH;
        private string POS_PATH;
        private string IDF_TRAIN_PATH;
        private string POS_TRAIN_PATH;

        private FileOperator fileReader = new FileOperator();

        public SvmFileOperator()
        {
            string rootPath = Path.GetDirectoryName(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory));
            CLASSIFIER_CONFIG_FILE = rootPath + Constants.SvmClassifierPath;
            CLASSIFIER_CONFIG_TRAIN_FILE = rootPath + Constants.SvmClassifierTrainPath;
            NONBOT_FOLDER_PATH = rootPath + Constants.NonbotTrainPath;
            BOT_FOLDER_PATH = rootPath + Constants.BotTrainPath;
            IDF_PATH = rootPath + Constants.VocabularyIDF;
            POS_PATH = rootPath + Constants.VocabularyPOS;
            IDF_TRAIN_PATH = rootPath + Constants.VocabularyIDFTrain;
            POS_TRAIN_PATH = rootPath + Constants.VocabularyPOSTrain;
        }
        public List<string> GetTrainDocuments(ref int firstCategoryNumber)
        {
            List<string> documents = new List<string>();

            documents.AddRange(fileReader.ReadFileSet(NONBOT_FOLDER_PATH));
            firstCategoryNumber = documents.Count();
            documents.AddRange(fileReader.ReadFileSet(BOT_FOLDER_PATH));

            return documents;
        }

        public void WriteProblemFile(List<string> problemFile)
        {
            fileReader.StringListToFile(CLASSIFIER_CONFIG_TRAIN_FILE, problemFile);
        }

        public svm_problem ReadProblemFile()
        {
            return ProblemHelper.ReadProblem(CLASSIFIER_CONFIG_FILE);
        }
        public void SaveTFIDF()
        {
            TFIDF.Save(IDF_TRAIN_PATH, POS_TRAIN_PATH);
        }

        public void LoadTFIDF()
        {
            TFIDF.Load(IDF_PATH, POS_PATH);
        }
    }
}
